// 通用脚本
$(function (){
    // console.log("通用脚本")
    $.getJSON("/api/user/getCurrentUser",data=>{
        console.log(data)
        let loginStatusStr = ''
        if(!data['data']){
            loginStatusStr = ` <a href="login.html" >请登录</a>
                        <a href="register.html" class="style-red">免费注册</a>`
        }else{
            // 获取成功 表示data有值
            loginStatusStr = `
                 ${data['data'].username}
                 <a href="javascript:logoutFun();" class="style-red">注销</a>
            `
        }
        $('.shortcut>.w>.fl>ul>li:last-child').html(loginStatusStr)
    })
})

function logoutFun(){
    // if(confirm("确认注销吗")){
    //     $.getJSON("/api/user/logout",data=>{
    //         console.log(data)
    //         alert(data['msg'])
    //         if(data['code']==1010){
    //             window.location.href = '/login.html'
    //         }
    //     })
    // }
    let layer = layui.layer
    layer.confirm('确认注销吗?', {
        btn: ['是','否'] //按钮
    }, function(){
        $.getJSON("/api/user/logout", data => {
            console.log(data)
            layer.msg(data['msg'], {
                time: 20000, //20s后自动关闭
                btn: ['知道了']
            },function (){
                if (data['code'] == 1010) {
                    window.location.href = '/login.html'
                }
            });

        })
    }, function(){
        layer.msg("取消注销")
    });
}